﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDOS.Objects;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using DDOS.Game_State;

namespace DDOS
{
    class TerminalLayer : ScreenLayer
    {
        private Player player;
        private Texture2D[] isSelected;
        private Texture2D[] notSelected;
        private bool[] selected;
        private int pnter = 0;
        private Vector2[] coord;
        

        public TerminalLayer(Player plyr) : base(false)
        {
            
            isSelected = new Texture2D[3];
            notSelected = new Texture2D[3];
            coord = new Vector2[3];

            isSelected[0] = ImageManager2D.Instance.IESelected;
            isSelected[1] =ImageManager2D.Instance.FolderSelected;
            isSelected[2] =ImageManager2D.Instance.DatabaseSelected;
            notSelected[0] = ImageManager2D.Instance.IE;
            notSelected[1] = ImageManager2D.Instance.NormalFolder;
            notSelected[2] = ImageManager2D.Instance.Database;
            coord[0] = new Vector2((float)(ScreenManager.Instance.ScreenBounds.Center.X - 275), ScreenManager.Instance.ScreenBounds.Top + 30);
            coord[1] = new Vector2((float)(ScreenManager.Instance.ScreenBounds.Center.X-75 ), ScreenManager.Instance.ScreenBounds.Top + 30);
            coord[2] = new Vector2((float)(ScreenManager.Instance.ScreenBounds.Center.X - 200), ScreenManager.Instance.ScreenBounds.Top + 30);
            selected = new bool[3];
            selected[0] = true;
            selected[1] = false;
            selected[2] = false;
            SoundManager.Instance.Volumn(0.2f);
            player = plyr;
            player.InitializeComponents();
        }

        public TerminalLayer() :base(false)
        {
            player = new Player();
            player.InitializeComponents();
            SoundManager.Instance.Volumn(0.5f);
            isSelected = new Texture2D[3];
            notSelected = new Texture2D[3];
            coord = new Vector2[3];

            isSelected[0] = ImageManager2D.Instance.IESelected;
            isSelected[1] = ImageManager2D.Instance.FolderSelected;
            isSelected[2] = ImageManager2D.Instance.DatabaseSelected;
            notSelected[0] = ImageManager2D.Instance.IE;
            notSelected[1] = ImageManager2D.Instance.NormalFolder;
            notSelected[2] = ImageManager2D.Instance.Database;
            coord[0] = new Vector2((float)(ScreenManager.Instance.ScreenBounds.Center.X - 400), ScreenManager.Instance.ScreenBounds.Top + 30);
            coord[1] = new Vector2((float)(ScreenManager.Instance.ScreenBounds.Center.X - 75), ScreenManager.Instance.ScreenBounds.Top + 30);
            coord[2] = new Vector2((float)(ScreenManager.Instance.ScreenBounds.Center.X + 200), ScreenManager.Instance.ScreenBounds.Top + 30);
            selected = new bool[3];
            selected[0] = true;
            selected[1] = false;
            selected[2] = false;
            
            SoundManager.Instance.LogonInstance.Volume = 1f;
            SoundManager.Instance.LogonInstance.Play();
        }

        
        public override void Update(Microsoft.Xna.Framework.GameTime gt)
        {


            if (InputManager.InputManagerInstance.KeyWasReleased(Keys.Left) || InputManager.InputManagerInstance.GamePadThumbstickMovedLeft(InputManager.GamePadThumbstick.left) || InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.DPadLeft))
            {
                selected[pnter] = false;
                pnter -= 1;

                if (pnter < 0)
                    pnter = 0;

                selected[pnter] = true;

            }

            if (InputManager.InputManagerInstance.KeyWasReleased(Keys.Right) || InputManager.InputManagerInstance.GamePadThumbstickMovedRight(InputManager.GamePadThumbstick.left) || InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.DPadRight))
            {
                selected[pnter] = false;
                pnter += 1;

                if (pnter > 2)
                    pnter = 2;

                selected[pnter] = true;

            }

            if (InputManager.InputManagerInstance.GamePadButtonWasReleased(Buttons.A) || InputManager.InputManagerInstance.KeyWasReleased(Keys.Enter))
            {
                if (selected[0])
                {
                    ScreenManager.Instance.CurrentScreen.PushLayer(new LevelSelectLayer(player));
                }
                if (selected[1])
                {
                    ScreenManager.Instance.CurrentScreen.PushLayer(new MainGambitLayer(player));
                }
                if (selected[2])
                {
                }
            }

        }

        public override void Draw()
        {
            ScreenManager.Instance.CurrentSpriteBatch.Draw(ImageManager2D.Instance.Terminal, Vector2.Zero, Color.White);
            
                for (int i = 0; i < 3; i++)
                {
                    if (selected[i] == true)
                        ScreenManager.Instance.CurrentSpriteBatch.Draw(isSelected[i], coord[i], Color.White);
                    else
                        ScreenManager.Instance.CurrentSpriteBatch.Draw(notSelected[i], coord[i], Color.White);
                }
            
        }
    }

}
